//
//  ESReadHistoryViewController.m
//  EasyStyle
//
//  Created by FK on 16/5/10.
//  Copyright © 2016年 FK. All rights reserved.
//

#import "ESReadHistoryViewController.h"
#import "ESWeChatReadHistoryDAO.h"
#import "ESWeChatTableViewcell.h"
#import "UIImageView+WebCache.h"
#import "ESWeiXinNewsDetailViewController.h"

@interface ESReadHistoryViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *newsList;

@property (nonatomic, strong) UITableView *tableView;



@end

@implementation ESReadHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
    [self setupRootView:self.view.frame];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark -- 加载数据
- (void)loadData
{
    ESWeChatReadHistoryDAO *dao = [ESWeChatReadHistoryDAO shareInstance];
    _newsList = nil;
    _newsList = [dao queryAllReadHistoryData];
}

#pragma mark -- 布局相关
- (void)setupRootView:(CGRect)parentFrame
{
    UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:parentFrame];
    bgImageView.image = [UIImage imageNamed:@"WeChatTableViewBG"];
    [self.view insertSubview:bgImageView atIndex:0];
    
    WS(weakSelf);
    self.navigationController.navigationBarHidden = YES;
    [self addCustomNavigationBarWithStyle:DefaultStyle customColor:[UIColor colorWithHex:@"3ec5dc"]];
    
    [self.sgCustomNavigationBar showBackButtonWithActionBlock:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
    [self.sgCustomNavigationBar setTitle:@"阅读历史"];
    self.sgCustomNavigationBar.titleLabel.textColor = [UIColor whiteColor];
    

    // 清空Btn
    UIButton *clearButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 88, 44)];
    clearButton.backgroundColor = [UIColor clearColor];
    [clearButton setTitle:@"清空" forState:UIControlStateNormal];
    [clearButton addTarget:self action:@selector(clearAllData) forControlEvents:UIControlEventTouchUpInside];
    [self.sgCustomNavigationBar setRightBarButtonView:clearButton];

    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.sgCustomNavigationBar.height, self.view.width, self.view.height-self.sgCustomNavigationBar.height-self.tabBarController.tabBar.height) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorInset = UIEdgeInsetsMake(0,13,0,13);
    _tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_tableView];
}

#pragma mark -- 事件相关
- (void)clearAllData
{
    ESWeChatReadHistoryDAO *dao = [ESWeChatReadHistoryDAO shareInstance];
    [dao deleteAllData];
    
    [_newsList removeAllObjects];
    
    [_tableView reloadData];
}


#pragma mark -- UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_newsList != nil && _newsList.count > 0) {
        return _newsList.count;
    } else {
        return 0;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ESWeChatTableViewcell *cell = [tableView dequeueReusableCellWithIdentifier:@"wechatHistoryNewsListCell"];
    if (cell == nil) {
        cell = [ESWeChatTableViewcell loadViewFromSameXibName];
        [cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
        cell.backgroundColor = [UIColor clearColor];
    }
    
    WxNewsObject *item = [_newsList objectAtIndex:indexPath.row];
    cell.newsObject = item;
    return cell;
}


#pragma mark -- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    WxNewsObject *item = [_newsList objectAtIndex:indexPath.row];
    if (item == nil || item.url == nil || item.url.length <= 0) {
        return;
    }
    
    ESWeiXinNewsDetailViewController *detailVC = [ESWeiXinNewsDetailViewController new];
    detailVC.isCanCollect = YES;
    detailVC.newsObject = item;
    detailVC.newsTitle = item.title;
    detailVC.newsURL = item.url;
    [self.navigationController pushViewController:detailVC animated:YES];
}


@end
